package com.itheima52.mobilesafe.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.utils.MD5Utils;

public class HomeActivity extends Activity {
	private SharedPreferences mSpref;
	private GridView gvHome;
	private String[] mItems = new String[] { "手机防盗", "通讯卫士", "软件管理", "进程管理",
			"流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心" };
	private int[] mPics = new int[] { R.drawable.home_safe,
			R.drawable.home_callmsgsafe, R.drawable.home_apps,
			R.drawable.home_taskmanager, R.drawable.home_netmanager,
			R.drawable.home_trojan, R.drawable.home_sysoptimize,
			R.drawable.home_tools, R.drawable.home_settings

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mSpref = getSharedPreferences("config", MODE_PRIVATE);
		gvHome = (GridView) findViewById(R.id.gv_home);
		gvHome.setAdapter(new HomeAdapter());
		gvHome.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					// 手机防盗
					showPasswordDialog();
					break;
				case 1:
					// 通讯卫士
					startActivity(new Intent(HomeActivity.this,
							CallSafeActivity2.class));
					break;
				case 2:
					// 软件管理
					startActivity(new Intent(HomeActivity.this,
							AppManagerActivity.class));
					break;
				case 3:
					// 进程管理
					startActivity(new Intent(HomeActivity.this,
							TaskManagerActivity.class));
					break;

				case 7:
					// 高级工具
					startActivity(new Intent(HomeActivity.this,
							AToolsActivity.class));

					break;
				case 8:
					// 设置中心
					startActivity(new Intent(HomeActivity.this,
							SettingActivity.class));

					break;

				default:
					break;
				}

			}
		});
	}

	/**
	 * 显示密码弹窗
	 */
	protected void showPasswordDialog() {
		// 判断是否设置过密码
		String savedPassword = mSpref.getString("password", null);
		if (!TextUtils.isEmpty(savedPassword)) {
			// 设置过密码,输入确定密码
			showPasswordInputDialog();
		} else {
			// 如果没有设置过密码,弹出设置密码的弹窗
			showPasswordSetDialog();
		}

	}

	/**
	 * 输入确定密码
	 */
	private void showPasswordInputDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
		final AlertDialog dialog = builder.create();
		View view = View.inflate(HomeActivity.this,
				R.layout.dailog_input_password, null);
		dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.3模拟器上运行没有问题
		// dialog.setView(view);//将自定义的布局文件设置给dialog
		View btnOk = view.findViewById(R.id.btn_ok);
		View btnCancel = view.findViewById(R.id.btn_cancel);
		final EditText etPassword = (EditText) view
				.findViewById(R.id.et_password);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String password = etPassword.getText().toString();

				if (!TextUtils.isEmpty(password)) {
					String savedPassword = mSpref.getString("password", null);
					if (MD5Utils.encode(password).equals(savedPassword)) {
						Toast.makeText(HomeActivity.this, "登陆成功",
								Toast.LENGTH_SHORT).show();
						dialog.dismiss();
						// 跳转到手机防盗页面
						startActivity(new Intent(HomeActivity.this,
								LostFindActivity.class));
					} else {
						Toast.makeText(HomeActivity.this, "密码不正确",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(HomeActivity.this, "输入框不能为空",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 隐藏dialog
				dialog.dismiss();

			}
		});
		dialog.show();

	}

	/**
	 * 初始化输入密码
	 */
	private void showPasswordSetDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
		final AlertDialog dialog = builder.create();
		View view = View.inflate(HomeActivity.this,
				R.layout.dailog_set_password, null);
		dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.3模拟器上运行没有问题
		// dialog.setView(view);//将自定义的布局文件设置给dialog
		View btnOk = view.findViewById(R.id.btn_ok);
		View btnCancel = view.findViewById(R.id.btn_cancel);
		final EditText etPassword = (EditText) view
				.findViewById(R.id.et_password);
		final EditText etPasswordConfirm = (EditText) view
				.findViewById(R.id.et_password_confirm);
		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 点击确定时
				String password = etPassword.getText().toString();
				String passwordConfirm = etPasswordConfirm.getText().toString();
				if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) {
					if (password.equals(passwordConfirm)) {
						Toast.makeText(HomeActivity.this, "登陆成功!",
								Toast.LENGTH_SHORT).show();
						mSpref.edit()
								.putString("password",
										MD5Utils.encode(password)).commit();

						dialog.dismiss();
						// 跳转到手机防盗页面
						startActivity(new Intent(HomeActivity.this,
								LostFindActivity.class));
					} else {
						Toast.makeText(HomeActivity.this, "两次密码不一致",
								Toast.LENGTH_SHORT).show();

					}
				} else {
					Toast.makeText(HomeActivity.this, "输入框都不能为空",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 隐藏dialog
				dialog.dismiss();

			}
		});
		dialog.show();
	}

	// GridView的适配器
	class HomeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mItems.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mItems[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(HomeActivity.this,
					R.layout.home_list_item, null);
			ImageView ivItem = (ImageView) view.findViewById(R.id.iv_item);
			TextView tvItem = (TextView) view.findViewById(R.id.tv_item);
			tvItem.setText(mItems[position]);
			ivItem.setImageResource(mPics[position]);
			return view;
		}

	}

}
